package com.app.controller;


import com.app.aspect.Log;
import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.service.AdminUserService;
import com.app.service.ISysLogService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/jtwq")
public class SysLogController {
    @Autowired
    private ISysLogService iSysLogService;
    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/selectLogList")
    public Result<Object> selectLogList(Integer page, Integer limit, String title, Integer adminId, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(iSysLogService.selectLogList(page, limit, title, adminId));
    }

    @GetMapping("/deleteLogById")
    @Log("日志删除")
    public Result<Object> deleteLogList(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        iSysLogService.deleteLogList(id);
        return Result.success("");
    }

    @GetMapping("/selectLogById")
    public Result<Object> selectLogById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        iSysLogService.selectLogById(id);
        return Result.success("");
    }

}

